﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Text;
using System.Data;
using System.Web.Configuration;

namespace ReconUI
{
    public partial class DiscountPenaltyMaintenance : System.Web.UI.Page
    {
        DBFactory db;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (((UserVO)Session["CurrentUserVO"]) == null)
            {
                Response.Redirect("ReconSignIn.aspx");
            }
            if (!Page.IsPostBack)
            {
                // GetUnmatchedLists();
                FillRemitterForMatching();
            }
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>window.open('SelectDate.aspx?textbox=" + txtEffectiveDt.ClientID + "','cal','width=250,height=225,left=480,top=280')</script>");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            db = new DBFactory();

            int retval = db.AddDiscountPenalty(Convert.ToString(rbOption.SelectedValue.Trim() == "D" ? "D" : "P"), Convert.ToString(ddlCustomer.SelectedValue.Trim()), ConvertDate(txtEffectiveDt.Value),
                    Convert.ToInt32(txtDayStart.Text.Trim()), Convert.ToInt32(txtDayEnd.Text.Trim()),
                    Convert.ToDouble(txtPercentage.Text.Trim()), Convert.ToDouble(txtMinAmount.Text.Trim()), Convert.ToDouble(txtMaxAmount.Text.Trim()));

            if (retval != 0)
            {
                lblMessage.Text = rbOption.SelectedItem.Text + " added successfully!";
                btnClickPopup.Show();
                ResetFields();
            }
            else
            {
                lblMessage.Text = "Error adding " + rbOption.SelectedItem.Text + " !";
                btnClickPopup.Show();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetFields();
        }

        public string ConvertDate(string yyyyMMdd)
        {
            string[] str;
            string s = yyyyMMdd;
            str = s.Split('-').ToArray();

            if (str[0].Length == 1)
            { str[0] = "0" + str[0]; }

            if (str[1].Length == 1)
            { str[1] = "0" + str[1]; }

            s = str[2] + "-" + str[1] + "-" + str[0];
            return s;

        }


        private void ResetFields()
        {
            rbOption.SelectedValue = "D";
            ddlCustomer.SelectedValue = "0";           
            txtEffectiveDt.Value = "";
            txtDayStart.Text = "";
            txtDayEnd.Text = "";
            txtPercentage.Text = "";
            txtMinAmount.Text = "";
            txtMaxAmount.Text = "";            
        }

        private void FillRemitterForMatching()
        {
            db = new DBFactory();
            DataTable dtRemitter = db.GetCustomer(string.Empty);

            if (dtRemitter != null && dtRemitter.Rows.Count > 0)
            {
                for (int i = 0; i < dtRemitter.Rows.Count; i++)
                {
                    ddlCustomer.Items.Add(dtRemitter.Rows[i].ItemArray[0].ToString().Trim() + " - " + dtRemitter.Rows[i].ItemArray[1].ToString().Trim());
                    ddlCustomer.Items[i + 1].Value = (dtRemitter.Rows[i].ItemArray[0].ToString().Trim());
                }
            }
        }
        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }

    }
}